package com.tchepannou.limbe.test;

import com.tchepannou.limbe.mail.MailController;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import java.io.IOException;
import java.io.Serializable;

/**
 * User: herve
 * Date: 14-05-10 12:16 PM
 */
public class MailControllerTestCase extends BaseTestCase
{
    //-- Attribute
    private MailController _mailController;


    //-- TestCase override
    @Override
    protected void setUp ()
        throws Exception
    {
        super.setUp();
    }


    //-- Public
    public MailController deliver (String path, Serializable data) throws IOException, MessagingException
    {
        _mailController = getEngine().getMailContainer().deliver(path, data);
        return _mailController;
    }

    public void assertThatViewVariableEquals (String name, Object expected)
    {
        Object value = _mailController.getViewVariable(name);
        assertEquals("mailController.viewVariable[" + name + "]", expected, value);
    }
    public void assertThatViewVariableNull (String name)
    {
        Object value = _mailController.getViewVariable(name);
        assertNull("mailController.viewVariable[" + name + "]", value);
    }
    public void assertThatViewVariableNotNull (String name)
    {
        Object value = _mailController.getViewVariable(name);
        assertNotNull("mailController.viewVariable[" + name + "]", value);
    }

    public void assertThatHasTo(String email)
    {
        for (InternetAddress addr : _mailController.getTo())
        {
            if (addr.getAddress().equals(email))
            {
                return;
            }
        }
        fail(email);
    }
    public void assertThatHasTo(String email, String displayName)
    {
        for (InternetAddress addr : _mailController.getTo())
        {
            if (addr.getAddress().equals(email) && displayName.equals(addr.getPersonal()))
            {
                return;
            }
        }
        fail(displayName + "<" + email + ">");
    }

    public void assertThatHasCc(String email)
    {
        for (InternetAddress addr : _mailController.getCc())
        {
            if (addr.getAddress().equals(email))
            {
                return;
            }
        }
        fail(email);
    }
    public void assertThatHasCc(String email, String displayName)
    {
        for (InternetAddress addr : _mailController.getCc())
        {
            if (addr.getAddress().equals(email) && displayName.equals(addr.getPersonal()))
            {
                return;
            }
        }
        fail(displayName + "<" + email + ">");
    }

    public void assertThatHasBcc(String email)
    {
        for (InternetAddress addr : _mailController.getBcc())
        {
            if (addr.getAddress().equals(email))
            {
                return;
            }
        }
        fail(email);
    }
    public void assertThatHasBcc(String email, String displayName)
    {
        for (InternetAddress addr : _mailController.getBcc())
        {
            if (addr.getAddress().equals(email) && displayName.equals(addr.getPersonal()))
            {
                return;
            }
        }
        fail(displayName + "<" + email + ">");
    }

    public void assertThatSubjectEquals(String subject)
    {
        assertEquals(subject, _mailController.getSubject());
    }

    public MailController getMailController ()
    {
        return _mailController;
    }
}
